package com.huluobo.a2204monthparkdemo.parking

import android.app.AlertDialog
import android.content.DialogInterface
import android.content.DialogInterface.OnClickListener
import android.content.Intent
import android.graphics.Color
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2204monthparkdemo.adapter.CarImgAdapter
import com.huluobo.a2204monthparkdemo.adapter.ColorAdapter
import com.huluobo.a2204monthparkdemo.databinding.ActivityParkingValueBinding
import com.huluobo.base.bean.ColorBean
import com.huluobo.base.bean.ParkingBeanItem
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

/**
 *  Created by LC on 2024/9/5.
 */
@AndroidEntryPoint
class ParkingValueActivity : BaseActivity<ActivityParkingValueBinding, ParkingViewModel>() {
    private lateinit var parkingBeanItem: ParkingBeanItem
    private lateinit var colorAdapter: ColorAdapter
    private lateinit var carImgAdapter: CarImgAdapter
    override fun initData() {
        viewModel.getCarColor()
        viewModel.getCarColorSuccess.observe(this) {
            val list = Gson().fromJson(it, ColorBean::class.java)
            colorAdapter.data.clear()
            colorAdapter.data.addAll(list)
            colorAdapter.notifyDataSetChanged()
        }

        viewModel.addParkingSuccess.observe(this) {
            Log.i("TagA", "添加车位成功:$it")
        }

        viewModel.deleteColorSuccess.observe(this) {
            ToastUtils.showLong("删除颜色成功")
            viewModel.getCarColor()
        }
    }

    override fun initView() {
        parkingBeanItem = intent.getSerializableExtra(Const.URL_PARKING) as ParkingBeanItem
        binding.parkingValueHeader.headerTitle.text = "编辑车位"
        binding.parkingValueHeader.headerBack.setOnClickListener { finish() }

        binding.parkingValueStartTimeNowTv.text = parkingBeanItem.parkStart
        binding.parkingValueEndTimeNowTv.text = parkingBeanItem.parkEnd
        binding.parkingValueDateNowTv.text = parkingBeanItem.parkTime
        binding.parkingValueUsernameTv.text = parkingBeanItem.parkName

        binding.parkingCarColorRv.layoutManager = LinearLayoutManager(this, RecyclerView.HORIZONTAL, false)
        colorAdapter = ColorAdapter()
        binding.parkingCarColorRv.adapter = colorAdapter

        colorAdapter.setOnItemClickListener { adapter, view, position ->
            binding.parkingCarColorSelectIv.setBackgroundColor(Color.parseColor(colorAdapter.data[position].colorValue))
        }

        colorAdapter.setOnItemLongClickListener { adapter, view, position ->
            AlertDialog.Builder(this)
                .setTitle("删除颜色")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", object : OnClickListener {
                    override fun onClick(dialog: DialogInterface?, which: Int) {
                        //删除颜色
                        val colorId = colorAdapter.data[position].colorId
                        val map = mutableMapOf<String, Int>()
                        map[Const.PARAM_COLOR_ID] = colorId
                        val json = Gson().toJson(map)
                        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                        viewModel.deleteColor(body)
                    }
                })
                .show()
            true
        }

        binding.parkingValueStartTimeIv.setOnClickListener {
            showDateDialog(binding.parkingValueStartTimeNowTv)
        }

        binding.parkingValueEndTimeIv.setOnClickListener {
            showDateDialog(binding.parkingValueEndTimeNowTv)
        }

        binding.parkingCarImgRv.layoutManager = LinearLayoutManager(this, RecyclerView.HORIZONTAL, false)
        carImgAdapter = CarImgAdapter()
        binding.parkingCarImgRv.adapter = carImgAdapter

        carImgAdapter.data.add("+")
        carImgAdapter.notifyDataSetChanged()

        carImgAdapter.setOnItemClickListener { adapter, view, position ->
            Log.i("TagA", "click:$position")
            if (position == adapter.data.size - 1) {
                PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE).selectPicture()
            }
        }

        binding.parkingCommitBtn.setOnClickListener {
            if (binding.parkingValueStartTimeNowTv.text.toString()
                    .isNotEmpty() && binding.parkingValueEndTimeNowTv.text.toString().isNotEmpty()
            ) {
                val map = mutableMapOf<String, Any>()
                map[Const.PARAM_PARK_ID] = parkingBeanItem.parkId
                map[Const.PARAM_USER_ID] = SPUtils.getInstance().getInt(Const.PARAM_USER_ID)
                map[Const.PARAM_PARK_NAME] = SPUtils.getInstance().getString(Const.PARAM_USERNAME)
                map[Const.PARAM_PARK_START] = binding.parkingValueStartTimeNowTv.text.toString()
                map[Const.PARAM_PARK_END] = binding.parkingValueEndTimeNowTv.text.toString()
                map[Const.PARAM_PARK_TIME] = getTime()

                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                viewModel.addParking(body)
            } else {
                ToastUtils.showLong("请选择开始时间和结束时间")
            }

        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK) {
            val builder = data?.extras
            val bean = builder?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            val filePath = bean?.path
            if (filePath != null) {
                carImgAdapter.data.add(0, filePath)
                carImgAdapter.notifyDataSetChanged()
            }
        }
    }
}